{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import astropy.units as u\n",
    "import warnings\n",
    "import time\n",
    "from astropy.coordinates import SkyCoord\n",
    "from astroquery.sdss import SDSS\n",
    "from astroquery.ned import Ned\n",
    "\n",
    "# def text(index_begin=0):\n",
    "data_original = pd.read_csv('irsa_catalog_search_results.tbl', sep='\\s+', skiprows=32, header=None,\n",
    "                            names=['name', 'band', 'ra', 'dec', 'raerr', 'decerr', 'flux', 'type'])\n",
    "# data_original=pd.read_csv('text.tbl',sep='\\s+',header=None,names=['name','band','ra','dec','raerr','decerr','flux','type'])\n",
    "index = len(data_original.index.values)\n",
    "\n",
    "\n",
    "# <MaskedColumn name='Type' dtype='object' description=\"NED's\n",
    "# Preferred Object Type: G, GPair, GTrpl, GGroup, GClstr, QSO, AbLS, RadioS, IrS, EmLS, UvES, XrayS, SN\"\n",
    "\n",
    "def detect(index_begin=0, interval=1000):\n",
    "\n",
    "    for i in range(index_begin, index):\n",
    "        print('The echo is', i, '/', index)\n",
    "        ra = data_original['ra'][i]  # 赤经\n",
    "        dec = data_original['dec'][i]  # 赤纬\n",
    "        try:\n",
    "            table_sdss = SDSS.query_crossid(SkyCoord(ra, dec, unit=(u.deg, u.deg)), radius=3 * u.arcsec,\n",
    "                                            data_release=15)\n",
    "            warnings.filterwarnings('ignore')\n",
    "            if table_sdss is None:\n",
    "                table_ned = Ned.query_region(SkyCoord(ra, dec, unit=(u.deg, u.deg)), radius=3 * u.arcsec,\n",
    "                                             equinox='J2000.0')\n",
    "                if len(table_ned) > 0:\n",
    "                    # data_original['type'][i] = table_ned['Type'].description\n",
    "                    data_original['type'][i] = table_ned['Type'][0]\n",
    "                else:\n",
    "                    continue\n",
    "            else:\n",
    "                data_original['type'][i] = table_sdss['type'][0]\n",
    "        except:\n",
    "            continue\n",
    "\n",
    "        if (i % interval) == 0 and i != 0 :   # 默认每查询500次保存一次文件\n",
    "            data_name = 'result_%s.csv' % (i)\n",
    "            data_original.to_csv(data_name, float_format=\"%.6f\", index=0)\n",
    "            print('The file %s has been saved.\\n' % data_name)\n",
    "            time.sleep(5)   # 提示信息延时5秒\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
